//
//  ClxTabbarController.swift
//  SwiftSina
//
//  Created by 七 on 2017/4/14.
//  Copyright © 2017年 Apple. All rights reserved.
//

import UIKit

class ClxTabbarController: UITabBarController {

    ///懒加载发布按钮
    lazy var composeBtn: UIButton? = {
        let composeBtn = UIButton()
        composeBtn.setImage(UIImage(named: "tabbar_compose_icon_add"), for: .normal)
        composeBtn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), for: .highlighted)
        composeBtn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), for: .normal)
        composeBtn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), for: .highlighted)
        composeBtn.addTarget(self, action: #selector(composeClick), for: .touchUpInside)
        return composeBtn
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
//        tabBar.tintColor = .orange
        addChildViewController(vc: HomeViewController(), imageName: "tabbar_home", title: "首页")
        addChildViewController(vc: MessageViewController(), imageName: "tabbar_message_center", title: "消息")
        addChildViewController(vc: NullViewController(), imageName: "", title: "")
        addChildViewController(vc: DiscoverViewController(), imageName: "tabbar_discover", title: "发现")
        addChildViewController(vc: ProfileViewController(), imageName: "tabbar_profile", title: "我")
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        //添加加号按钮
        setupComposeButton()
    }
    
    fileprivate func addChildViewController(vc: UIViewController, imageName: String, title: String) {
        vc.tabBarItem.title = title
        vc.tabBarItem.image = UIImage(named: imageName)?.withRenderingMode(.alwaysOriginal)
        vc.tabBarItem.selectedImage = UIImage(named: imageName + "_highlighted")?.withRenderingMode(.alwaysOriginal)
        let nav = UINavigationController(rootViewController: vc)
        addChildViewController(nav)
    }
    //MARK:- 添加发布按钮
    fileprivate func setupComposeButton() {
        tabBar.addSubview(composeBtn!)
        //调整加号按钮的位置
//        composeBtn?.sizeToFit()
        let width = ScreenWidth / CGFloat(tabBar.subviews.count)
        composeBtn?.frame.size = CGSize(width: width+20, height: 49)
        composeBtn?.center = CGPoint(x: tabBar.center.x, y: tabBar.frame.size.height * 0.5)
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}
//MARK:- 点击事件
extension ClxTabbarController {
    @objc func composeClick() {
        print("发布 发布 发布")
    }
}
